/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.states;

import PacketHandling.PacketHandler;
import horizon.graphics.RenderingContext;
import horizon.gui.TitleScreenGui;

/**
 *
 * @author J_Laptop
 */
public class GameTitleScreenState extends State {

    TitleScreenGui gui;
    long timeOut = 5000;
    long time = 0;
    long authWait = 500;
    long authTime = 0;
    boolean success = false;
    boolean connectButton = false;
    boolean registerRequest = false;
    boolean authorized = false;

    public GameTitleScreenState(String s) {

        super(s);

    }

    @Override
    public void init() {

        gui = new TitleScreenGui(this);

    }

    @Override
    public void render(RenderingContext r) {

        gui.render(r);


    }

    @Override
    public void update(long elapsedTime) {

        if (success == false && connectButton) {

            if (time > timeOut) {

                time = 0;
                connect();

            } else {

                time += elapsedTime;

            }

        } else if (success && connectButton && registerRequest) {

            PacketHandler.getInstance().sendRegisterPacket("");
            registerRequest = false;

        }
        if (authorized) {

            if (authTime > authWait) {

                StateManager.getInstance().pushAndRemovePrevious(new GameLoadingState("Loading"));

            } else {
                
                authTime += elapsedTime;
                
            }

        }
        gui.update(elapsedTime);

    }

    private void connect() {

        gui.status.addLine("Connecting...");

        Thread connectAttempt = new Thread() {

            @Override
            public void run() {
                if (PacketHandler.getInstance().connect()) {

                    gui.status.addLine("Successfully Connected to Server...");
                    success = true;

                } else {

                    gui.status.addLine("Failed to Connect to Server!");
                    success = false;

                }

                if (success) {

                    gui.status.addLine("Authorizing client...");
                    registerRequest = true;

                }

            }
        };
        connectAttempt.setName("Connect");
        connectAttempt.start();

    }

    @Override
    public void actionGui(String s) {
        if (s == null) {

            return;

        }
        if (s.equals("connect") && connectButton != true) {

            connectButton = true;
            connect();

        }


    }

    public void addUpdateLine(String n) {

        gui.status.addLine(n);

    }

    public void clientAuthorized() {

        gui.status.addLine("Received Authorization. Welcome " + PacketHandler.getInstance().getName() + "!");
        authorized = true;

    }
}
